<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MapController
 *
 * @author phuong
 */
class MapController extends AppController {

    public function beforeFilter() {
        parent::beforeFilter();
        $this->loadModel('Building');
        $this->loadModel('Camera');
    }

    public function index() {
//        $custId = $this->Auth->user('CustId');
//        $camId = null;
//        if ($this->request->data) {
//            echo '<pre>';
//            print_r($this->request->data);
//            die;
//            if (!empty($this->request->data['Camera']['CustId']))
//                $custId = $this->request->data['Camera']['CustId'];
//            if (!empty($this->request->data['Camera']['CamId']))
//                $camId = $this->request->data['Camera']['CamId'];
//        }
        
        $buildings = $this->Camera->getAllCamera($this->request->data);
//        echo '<pre>';
//        print_r($buildings);
//        die;
        if (count($buildings) == 1) {
            $map_options = array(
                "id" => "map_canvas",
                "width" => "100%",
                "height" => "100%",
                "zoom" => 16,
                "type" => "ROADMAP",
                "localize" => false,
                "address" => $buildings[0]['Building']['BuildAddress'],
                "marker" => true,
                "markerIcon" => "http://maps.google.com/mapfiles/marker.png",
                "markerShadow" => "http://maps.google.com/mapfiles/shadow50.png",
                "infoWindow" => true,
                "markerTitle" => $buildings[0]['Camera']['CamName'],
            );
            $this->set('map_options', $map_options);
        }
        if (count($buildings) > 1) {
            $data = array();
            $map_options = array(
                "id" => "map_canvas",
                "width" => "100%",
                "height" => "100%",
                "zoom" => 10,
                "type" => "ROADMAP",
                "localize" => false,
                "address" => $buildings[0]['Building']['BuildAddress'],
                "marker" => true,
                "markerIcon" => "http://maps.google.com/mapfiles/marker.png",
                "markerShadow" => "http://maps.google.com/mapfiles/shadow50.png",
                "infoWindow" => true,
                "markerTitle" => $buildings[0]['Camera']['CamName'],
            );
            $add_options = array();
            unset($buildings[0]);
            $data['map_options'] = $map_options;
            $data['buildings'] = $buildings;
            $this->set($data);
        }
        if(count($buildings)==0){
             $map_options = array(
                "id" => "map_canvas",
                "width" => "100%",
                "height" => "100%",
                "zoom" => 16,
                "type" => "ROADMAP",
                "localize" => false,
                "address" => $this->Auth->user('CustName'),
                "marker" => true,
                "markerIcon" => "http://maps.google.com/mapfiles/marker.png",
                "markerShadow" => "http://maps.google.com/mapfiles/shadow50.png",
                "infoWindow" => true,
                "markerTitle" => $this->Auth->user('CustAddress'),
            );
            $this->set('map_options', $map_options);
        }
    }

}
